package com.rxkj.service.impl;

import com.rxkj.entity.sys.RoleEntity;
import com.rxkj.entity.sys.UserInfo;
import com.rxkj.mapper.UserMapper;
import com.rxkj.service.UserService;
import com.rxkj.utils.MD5Util;
import com.rxkj.utils.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserInfo getUserById(Integer Id) {
        return userMapper.getUserById(Id);
    }

    @Override
    public UserInfo getUserByName(String userName) {
        return userMapper.getUserByName(userName);
    }

    @Override
    public int editPassword(Integer id, String newPassword) {
        return userMapper.editPassword(id, newPassword);
    }

    @Override
    public PageData<UserInfo> getUser(int currentPage, int pageSize, UserInfo userInfo) {
        Integer totalRows = userMapper.getTotalRows(userInfo);
        PageData<UserInfo> res = new PageData<UserInfo>(currentPage, pageSize, Long.valueOf(totalRows));
        List<UserInfo> qe = userMapper.getTotalUser(res.getStart(), res.getEnd(), userInfo);
        res.setRows(qe);
        return res;
    }

    @Override
    public PageData<RoleEntity> getRole(int currentPage, int pageSize, RoleEntity roleEntity) {
        Integer totalRows = userMapper.getTotalRole(roleEntity);
        PageData<RoleEntity> res = new PageData<RoleEntity>(currentPage, pageSize, (long) totalRows);
        List<RoleEntity> qe = userMapper.getRole(res.getStart(), res.getEnd(), roleEntity);
        res.setRows(qe);
        return res;
    }

    @Override
    public RoleEntity getByIdRole(Integer id) {
        return userMapper.getByIdRole(id);
    }

    @Override
    public int updateRole(RoleEntity roleEntity) {
        return userMapper.updateByIdRole(roleEntity);
    }

    @Transactional
    @Override
    public void addRole(RoleEntity roleEntity) {
        roleEntity.setRoleCode(roleEntity.getRoleName());
        userMapper.addRole(roleEntity);
        userMapper.addRoleMenus(roleEntity.getId(), userMapper.getMenuId());
    }

    @Transactional
    @Override
    public void deleteRole(Integer id) {
        userMapper.deleteRolemenu(id);
        userMapper.deleteRoleUser(id);
        userMapper.deleteRole(id);
    }

    @Override
    public int AddUser(UserInfo userInfo) {
        userInfo.setUserPwd(MD5Util.MD5(userInfo.getUserPwd()));
        userInfo.setCreateTime(new Date());
        return userMapper.insert(userInfo);
    }

    @Override
    public UserInfo getByIdUser(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public int updateUser(UserInfo userInfo) {
        return userMapper.updateByIdUser(userInfo);
    }

    @Override
    public int deleteUser(Integer id) {
        return userMapper.deleteUser(id);
    }


}
